<template>
<div class="search">
    <add v-if="currView=='add'" @close="currView='index'" @submited="submited" />
    <edit v-if="currView=='edit'" @close="currView='index'" @submited="submited" :data="formData" />
    <Card v-show="currView=='index'">
        <Row v-show="openSearch" @keydown.enter.native="handleSearch">
            <Form ref="searchForm" :model="searchForm" inline :label-width="0">
                <Form-item ref="searchForm" :model="searchForm" inline :label-width="0" style="display:flex;">
                    <Form-item label="" prop="title">
                        <Input type="text" v-model="searchForm.title" placeholder="合同标题" clearable style="width: 150px" />
                    </Form-item>
                    <Form-item label="" prop="firstParty">
                        <Input type="text" v-model="searchForm.firstParty" placeholder="甲方" clearable style="width: 150px" />
                    </Form-item>
                    <Form-item label="" prop="secondParty">
                        <Input type="text" v-model="searchForm.secondParty" placeholder="乙方" clearable style="width: 150px" />
                    </Form-item>
                    <Form-item label="" prop="signStatus">
                        <Select v-model="searchForm.signStatus" placeholder="签订状态" clearable style="width: 150px">
                            <Option value="">全部</Option>
                            <Option value="未签订">未签订</Option>
                            <Option value="已签订">已签订</Option>
                        </Select>
                    </Form-item>
                    <Form-item style="margin-left:10px;" class="br">
                        <Button @click="handleSearch" type="primary" icon="ios-search" size="small" ghost>搜索</Button>
                        <Button @click="handleReset" type="warning" size="small" icon="md-refresh" ghost>重置</Button>
                        <!-- <Button @click="add" type="info" size="small" icon="md-add" ghost>添加</Button>
                        <Button @click="delAll" type="error" icon="md-trash" size="small" ghost>删除</Button> -->
                        <Button @click="excelData" type="success" icon="md-paper-plane" size="small" ghost>导出</Button>
                    </Form-item>
                    <Form-item style="position:fixed;right:50px;top:130px">
                        <Button type="info" @click="showFilterPanelFlag = !showFilterPanelFlag" class="showFilterPanelFlag" icon="md-settings" size="small" ghost>
                            列筛选</Button>
                        <Button type="warning" @click="modal1 = true" class="showFilterPanelFlag" icon="ios-help-circle-outline" size="small" ghost>
                            使用教程</Button>
                        <Modal v-model="modal1" title="使用教程">
                            <p>1.XXXXXXXXXXXXXXXXXXXXXXXX</p>
                            <p>2.XXXXXXXXXXXXXXXXXXXXXXXX</p>
                            <p>3.XXXXXXXXXXXXXXXXXXXXXXXX</p>
                        </Modal>
                    </Form-item>
                </Form-item>
            </Form>
        </Row>
        <Row class="operation" style="position:relative;">
            <transition>
                <div v-show="showFilterPanelFlag" class="filter-panel">
                    <CheckboxGroup v-model="selected">
                        <div v-for="item in mycolumns" :key="item.key">
                            <Checkbox :label="item.title" style="margin: 2px 5px"></Checkbox>
                        </div>
                    </CheckboxGroup>
                </div>
            </transition>
        </Row>
        <Row v-show="openTip"> </Row>
        <Row>
            <Table :loading="loading" :height="tableHeight" border stripe size="small" :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort" @on-selection-change="changeSelect" @on-row-click="rowClick" :row-class-name="rowClassNmae"></Table>
        </Row>
        <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[15,20,50]" size="small" show-total show-elevator show-sizer></Page>
        </Row>
    </Card>
    <Modal v-model="signModal" title="合同签订上传" :mask="false" footer-hide draggable width="1000">
        <Row :gutter="16">
<!--            <Col span="24">-->
<!--            <upload-file-input v-model="signFile" placeholder="请上传合同文件..." accept=".pdf" style="width:100%"></upload-file-input>-->
<!--            </Col>-->
            <Col span="24">
            <Divider>
                <Button @click="signAgreementFx" :loading="signLoading" type="primary">提交并保存</Button>
            </Divider>
            </Col>
        </Row>
    </Modal>
</div>
</template>

<script>
import {
  getAgreementList,
  deleteAgreement,
  auditAgreement,
  notAuditAgreement,
  signAgreement, exportWordFile
} from "./api.js";
import add from "./add.vue";
import edit from "./edit.vue";
import uploadFileInput from "@/views/template/upload-file-input";
export default {
    name: "single-window",
    components: {
        add,
        edit,
        uploadFileInput
    },
    data() {
        return {
            signModal: false,
            signFile: "",
            signLoading: false,
            signObj: {},
            tableHeight: 0,
            selected: [
                "选择",
                "序号",
                "合同提醒",
                "合同标题",
                "甲方",
                "乙方",
                "签订状态",
                "签订时间",
                "发起人",
                "操作",
            ],
            modal1: false,
            openSearch: true, // 显示搜索
            openTip: true, // 显示提示
            formData: {},
            currView: "index",
            loading: true, // 表单加载状态
            searchForm: { // 搜索框初始化对象
                pageNumber: 1, // 当前页数
                pageSize: 15, // 页面大小
                sort: "createTime", // 默认排序字段
                order: "desc", // 默认排序方式
                auditStatus: "已审批"
            },
            selectList: [], // 多选数据
            selectCount: 0, // 多选计数
            selectRow: 0,
            columns: [
                // 表头
                {
                    type: "selection",
                    width: 60,
                    title: "选择",
                    align: "center",
                    fixed: "left",
                },
                {
                    title: "序号",
                    width: 85,
                    align: "center",
                    fixed: "left",
                    sortType: true,
                    render: (h, params) => {
                        return h(
                            "span",
                            params.index +
                            (this.searchForm.pageNumber - 1) * this.searchForm.pageSize +
                            1
                        );
                    },
                },
                {
                  title: "合同提醒",
                  key: "dateTemp",
                  minWidth: 180,
                  tooltip: true,
                  sortable: false,
                  render: (h, params) => {
                    let content;
                    let style = {};

                    if (params.row.dateTemp === 0) {
                      // 如果 dateTemp 为 0，显示红色
                      content = "剩余 0 天";
                      style = { color: 'red' };
                    } else if (params.row.dateTemp < 0) {
                      // 如果 dateTemp 小于 0，显示“已到期”
                      content = "已到期";
                      style = { color: 'grey' }; // 可以选择适合的颜色
                    } else {
                      // 否则显示正常的剩余天数
                      content = "剩余 " + params.row.dateTemp + " 天";
                    }

                    return h(
                        "span",
                        {
                          style: style, // 应用样式
                        },
                        content // 显示内容
                    );
                  },
                },
                {
                    title: "合同标题",
                    key: "title",
                    minWidth: 180,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "甲方",
                    key: "firstParty",
                    minWidth: 160,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "乙方",
                    key: "secondParty",
                    minWidth: 160,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "合同原文件",
                    key: "file1",
                    minWidth: 150,
                    tooltip: true,
                    sortable: false,
                    render: (h, params) => {
                        if (params.row.file1 == undefined || params.row.file1 == "") {
                            return h("div", [
                                h(
                                    "span", {
                                        style: {
                                            color: "#ff9900",
                                        },
                                    },
                                    "未上传"
                                ),
                            ]);
                        } else {
                            return h("img", {
                                attrs: {
                                    src: require("@/assets/pdf.png"),
                                },
                                style: {
                                    cursor: "zoom-in",
                                    width: "60px",
                                    margin: "10px 0",
                                    "object-fit": "contain",
                                },
                                on: {
                                    click: () => {
                                        this.showFile(params.row.file1);
                                    },
                                },
                            });
                        }
                    }
                },
                {
                    title: "审批状态",
                    key: "auditStatus",
                    minWidth: 150,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "审批人",
                    key: "auditUser",
                    minWidth: 150,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "审批时间",
                    key: "auditTime",
                    minWidth: 180,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "签订状态",
                    key: "signStatus",
                    minWidth: 150,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "合同文件",
                    key: "file2",
                    minWidth: 150,
                    tooltip: true,
                    sortable: false,
                    render: (h, params) => {
                        if (params.row.file2 == undefined || params.row.file2 == "") {
                            return h("div", [
                                h(
                                    "span", {
                                        style: {
                                            color: "#ff9900",
                                        },
                                    },
                                    "未上传"
                                ),
                            ]);
                        } else {
                            return h("img", {
                                attrs: {
                                    src: require("@/assets/pdf.png"),
                                },
                                style: {
                                    cursor: "zoom-in",
                                    width: "60px",
                                    margin: "10px 0",
                                    "object-fit": "contain",
                                },
                                on: {
                                    click: () => {
                                        this.showFile(params.row.file2);
                                    },
                                },
                            });
                        }
                    }
                },
                {
                    title: "签订时间",
                    key: "signTime",
                    minWidth: 180,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "发起人ID",
                    key: "signId",
                    minWidth: 180,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "发起人",
                    key: "signUser",
                    minWidth: 150,
                    tooltip: true,
                    sortable: false,
                },
                {
                    title: "创建时间",
                    key: "createTime",
                    sortable: true,
                    sortType: "desc",
                    minWidth: 180,
                    align: "center",
                    tooltip: true,
                },
                {
                    title: "创建者",
                    key: "createBy",
                    sortable: true,
                    sortType: "desc",
                    minWidth: 100,
                    align: "center",
                    tooltip: true,
                },
                {
                    title: "修改时间",
                    key: "updateTime",
                    minWidth: 180,
                    align: "center",
                    tooltip: true,
                },
                {
                    title: "修改者",
                    key: "updateBy",
                    minWidth: 100,
                    align: "center",
                    tooltip: true,
                },
                {
                    title: "操作",
                    key: "action",
                    align: "center",
                    width: 110,
                    fixed: "right",
                    render: (h, params) => {
                        return h("div", [
                            h(
                                "Button", {
                                    props: {
                                        type: "primary",
                                        size: "small",
                                        icon: "ios-create-outline",
                                        ghost: true
                                    },
                                    style: {
                                        marginRight: "5px"
                                    },
                                    on: {
                                        click: () => {
                                            this.edit(params.row);
                                        }
                                    }
                                },
                                "详情"
                            ),
                            h(
                                "Button", {
                                  props: {
                                    type: "success",
                                    size: "small",
                                    icon: "md-download",
                                    ghost: true
                                  },
                                  style: {
                                    marginRight: "1px"
                                  },
                                  on: {
                                    click: () => {
                                      exportWordFile(params.row).then(res => {
                                      })
                                    }
                                  }
                                },
                                "下载"
                            )
                        ]);
                    }
                }
            ],
            data: [], // 表单数据
            pageNumber: 1, // 当前页数
            pageSize: 10, // 页面大小
            total: 0, // 表单数据总数
            showFilterPanelFlag: false,
        };
    },
    methods: {
        init() {
            this.getDataList();
        },
        signAgreementFx() {
            var that = this;
            that.signLoading = true;
            signAgreement({
                id: that.signObj.id,
                fileUrl: that.signFile
            }).then(res => {
                that.signLoading = false;
                that.signModal = false;
                if (res.success) {
                    that.getDataList();
                    this.$Message.success("签订成功");
                }
            })
        },
        showFile(e) {
            window.open(e + "?preview=true");
        },
        submited() {
            this.currView = "index";
            this.getDataList();
        },
        changePage(v) {
            this.searchForm.pageNumber = v;
            this.getDataList();
            this.clearSelectAll();
        },
        changePageSize(v) {
            this.searchForm.pageSize = v;
            this.getDataList();
        },
        rowClick(row, index) {
            this.selectRow = row;
        },
        rowClassNmae(row, index) {
            if (row.id == this.selectRow.id) {
                return "rowClassNmaeColor";
            }
            return "";
        },
        excelData() {
            this.$refs.table.exportCsv({
                filename: "导出结果",
            });
        },
        handleSearch() {
            this.searchForm.pageNumber = 1;
            this.searchForm.pageSize = 15;
            this.searchForm.auditStatus = "已审批";
            this.getDataList();
        },
        handleReset() {
            this.$refs.searchForm.resetFields();
            this.searchForm.auditStatus = "已审批";
            this.searchForm.pageNumber = 1;
            this.searchForm.pageSize = 15;
            // 重新加载数据
            this.getDataList();
        },
        changeSort(e) {
            this.searchForm.sort = e.key;
            this.searchForm.order = e.order;
            if (e.order === "normal") {
                this.searchForm.order = "";
            }
            this.getDataList();
        },
        clearSelectAll() {
            this.$refs.table.selectAll(false);
        },
        changeSelect(e) {
            this.selectList = e;
            this.selectCount = e.length;
        },
        getDataList() {
            this.loading = true;
            getAgreementList(this.searchForm).then(res => {
                this.loading = false;
                if (res.success) {
                    this.data = res.result.records;
                    this.total = res.result.total;
                }
            });
        },
        add() {
            this.currView = "add";
        },
        edit(v) {
            // 转换null为""
            for (let attr in v) {
                if (v[attr] == null) {
                    v[attr] = "";
                }
            }
            let str = JSON.stringify(v);
            let data = JSON.parse(str);
            this.formData = data;
            this.currView = "edit";
        },
        auditAgreementFx(v) {
            this.$Modal.confirm({
                title: "确认审核",
                content: "您确认要审核通过 ?",
                loading: true,
                onOk: () => {
                    auditAgreement({
                        id: v.id
                    }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success("审核成功");
                            this.getDataList();
                        }
                    });
                }
            });
        },
        notAuditAgreementFx(v) {
            this.$Modal.confirm({
                title: "确认驳回",
                content: "您确认要审核驳回 ?",
                loading: true,
                onOk: () => {
                    notAuditAgreement({
                        id: v.id
                    }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success("驳回成功");
                            this.getDataList();
                        }
                    });
                }
            });
        },
        remove(v) {
            this.$Modal.confirm({
                title: "确认删除",
                content: "您确认要删除 ?",
                loading: true,
                onOk: () => {
                    deleteAgreement({
                        ids: v.id
                    }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success("操作成功");
                            this.getDataList();
                        }
                    });
                }
            });
        },
        delAll() {
            if (this.selectCount <= 0) {
                this.$Message.warning("您还未选择要删除的数据");
                return;
            }
            this.$Modal.confirm({
                title: "确认删除",
                content: "您确认要删除所选的 " + this.selectCount + " 条数据?",
                loading: true,
                onOk: () => {
                    let ids = "";
                    this.selectList.forEach(function (e) {
                        ids += e.id + ",";
                    });
                    ids = ids.substring(0, ids.length - 1);
                    // 批量删除
                    deleteAgreement({
                        ids: ids
                    }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success("操作成功");
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        }
    },
    mounted() {
        this.init();
        this.tableHeight = Number(window.innerHeight - 273);
        this.mycolumns = this.columns;
        let showcolumns = [];
        for (var i = 0; i < this.selected.length; i++) {
            var item = this.selected[i];
            for (var j = 0; j < this.columns.length; j++) {
                if (this.columns[j].title == item) {
                    showcolumns.push(this.columns[j]);
                }
            }
        }
        this.columns = showcolumns;
    },
    watch: {
        selected: function (newcolumns) {
            let showcolumns = [];
            for (var i = 0; i < this.mycolumns.length; i++) {
                var item = this.mycolumns[i];
                if (item.title == undefined) showcolumns.push(item);
                else if (newcolumns.contains(item.title)) showcolumns.push(item);
            }
            this.columns = showcolumns;
        },
    },
};
</script>

<style lang="less">
// @import "../../../styles/table-common.less";
.search {
    .operation {
        margin-bottom: 2vh;
    }

    .select-count {
        font-weight: 600;
        color: #40a9ff;
    }

    .select-clear {
        margin-left: 10px;
    }

    .page {
        margin-top: 2vh;
    }

    .drop-down {
        margin-left: 5px;
    }
}

.filter-panel {
    width: 166px;
    min-height: 120px;
    height: 200px;
    position: absolute;
    background-color: white;
    z-index: 9999;
    margin-left: 1px;
    overflow-y: scroll;
    border: 1px solid blue;
    top: 35px;
    right: 10px;
}

.openSearch {
    position: absolute;
    right: 240px;
}

.openTip {
    position: absolute;
    right: 130px;
}

.showFilterPanelFlag {
    position: static !important;
    right: 10px;
    margin-right: 10px;
}

.ivu-table td {
    height: 38px !important;
}

.ivu-table-cell-with-expand {
    height: 38px !important;
    line-height: 38px !important;
}

.ivu-table .rowClassNmaeColor td {
    background-color: #b0b3b6 !important;
    color: #ffffff !important;
    font-size: 12px;
}
</style>
